/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSTextView.h"
#include "NSKeyedUnarchiver.h"

class NSTextViewSharedData: public NSObject
{
	DECLARE_DYNAMIC_CLASS(NSTextViewSharedData)

public:
	NSTextViewSharedData() : NSObject()
	{
	}
	virtual void initWithUnarchiver(NSKeyedUnarchiver *archiver)
	{
		NSObject::initWithUnarchiver(archiver);

		archiver->decodeObjectForKey(wxT("NSBackgroundColor"));
		archiver->decodeObjectForKey(wxT("NSDefaultParagraphStyle"));
		archiver->decodeIntForKey(wxT("NSFlags"));
		archiver->decodeObjectForKey(wxT("NSInsertionColor"));
		archiver->decodeObjectForKey(wxT("NSLinkAttributes"));
		archiver->decodeObjectForKey(wxT("NSMarkedAttributes"));
		archiver->decodeObjectForKey(wxT("NSSelectedAttributes"));
	}
};
IMPLEMENT_DYNAMIC_CLASS(NSTextViewSharedData, NSObject)


IMPLEMENT_DYNAMIC_CLASS(NSTextView, NSView)

NSTextView::NSTextView() : NSView(), _textfield(NULL)
{
	exposeBinding(wxT("value"));
}

void NSTextView::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
 	NSView::initWithUnarchiver(archiver);

	wxObject* deleg = archiver->decodeObjectForKey(wxT("NSDelegate"));
	wxObject* shared_data = archiver->decodeObjectForKey(wxT("NSSharedData"));

	if (archiver->contains(wxT("NSMinSize")))
		archiver->decodeSizeForKey(wxT("NSMinSize"));
	else if (archiver->contains(wxT("NSMinize")))
		archiver->decodeSizeForKey(wxT("NSMinize"));
	if (archiver->contains(wxT("NSMaxSize")))
		archiver->decodeSizeForKey(wxT("NSMaxSize"));

	if (archiver->contains(wxT("NSTVFlags")))
		archiver->decodeIntForKey(wxT("NSTVFlags"));
}

void NSTextView::_setValue(const wxString& key, wxObject *value)
{
	if (key == wxT("value"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_value = v->GetString();
		if (_textfield)
			_textfield->ChangeValue(_value);
	} else
		NSView::_setValue(key, value);
}
wxObject* NSTextView::_getValue(const wxString& key)
{
	if (key == wxT("value"))
	{
		return new wxVariant(_value);
	} else
		return NSView::_getValue(key);
}

void NSTextView::_releaseWidget(void)
{
	if (_textfield)
	{
		_textfield->Destroy();
		_textfield = NULL;
	}
}

wxWindow* NSTextView::_allocWidget(NSView* parent)
{
	wxASSERT(!_textfield);
	wxWindow* _parent = parent->widget();
	wxASSERT(_parent);
	_textfield = new wxTextCtrl(_parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
	_textfield->SetEditable(false);
	_textfield->ChangeValue(_value);
	_initWidget(_textfield);
	return _textfield;
}

void NSTextView::setSize(const wxRect& newSize)
{
	NSView::setSize(newSize);
	if (_textfield)
		_initWidget(_textfield);
}
